package Assignment12_Maze;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import Scrap.Maze;

import java.util.Scanner;

public class Assignment12_Maze {

	boolean roadblock = false;
	int start_row = 0;
	int start_col = 0;
	int rows;
	int cols;
	char[][] maze;
	static int hops = 0;

	// boolean found;

	public static void main(String[] args) throws FileNotFoundException {

		Assignment12_Maze m = new Assignment12_Maze();

		Scanner sc = new Scanner(System.in);
		// Scanner sc = new Scanner(new File(args[0]));

		String num_inp = sc.nextLine();
		Scanner sc1 = new Scanner(num_inp);
		m.rows = sc1.nextInt();
		m.cols = sc1.nextInt();

		// System.out.println(m.rows + " :: " + m.cols);

		m.maze = new char[m.rows][m.cols];

		while (sc.hasNext()) {

			for (int i = 0; i < m.maze.length; i++) {

				String input = sc.nextLine();
				// System.out.println(input);
				// String mod_input = input.replace(" ", "");

				// System.out.println("input :: " + input);

				String mod_input = input.replace(" ", "");

				for (int j = 0; j < m.maze[i].length; j++) {

					m.maze[i][j] = mod_input.charAt(j);

					if (m.maze[i][j] == '#') {

						m.start_row = i;
						m.start_col = j;

					}

				}

			}

		}

		printMaze(m.maze);

		// try {

		m.analyseMaze(m.start_row, m.start_col ,-1,-1);
		System.out.println("hops :: " + hops);
		// } catch (Exception e) {
		//
		// }
		//printMaze(m.maze);

	}

	private boolean analyseMaze(int sr, int sc, int prevI, int prevJ) {

		// Thread.sleep(200);
		System.out.println(sr + " " + sc);

		// this logic appears correct
		
		if (inBounds(sr,sc-1) && maze[sr][sc - 1] == '@') {//west

			hops = hops + 1;
			return true;

		} else if (inBounds(sr-1,sc) && maze[sr - 1][sc] == '@') {//north

			hops = hops + 1;
			return true;

		} else if (inBounds(sr,sc+1) && maze[sr][sc + 1] == '@') {//east

			hops = hops + 1;
			return true;

		} else if (inBounds(sr+1,sc) && maze[sr + 1][sc] == '@') {//south

			hops = hops + 1;
			return true;

		}
		 
		// System.out.println("exit obtained :: " + found);

	//	maze[sr][sc] = '*';

		// if (maze[sr][sc] == '@') {
		//
		// found = true;
		//
		//
		// }

		// this one needs a check

		// && (maze[sr][sc - 1] != '#'

		boolean found = false;
		if ( !(sr==prevI && (sc-1)==prevJ) && inBounds(sr,sc-1) && (maze[sr][sc - 1] == '1')) {

			found = analyseMaze(sr, sc - 1, sr, sc);//west
			if (found) {
				hops = hops + 1;
				return true;
			}

		}
		if (!found) {

			if (!(sr-1==prevI && (sc)==prevJ) && inBounds(sr-1,sc) &&(maze[sr - 1][sc] == '1')) {

				found = analyseMaze(sr - 1, sc, sr, sc);//north
				if (found) {
					hops = hops + 1;
					return true;
				}

			}
		}

		if (!found) {
			if (!(sr==prevI && (sc+1)==prevJ) &&   inBounds(sr,sc+1) &&(maze[sr][sc + 1] == '1')) {

				found = analyseMaze(sr, sc + 1, sr, sc);//east
				if (found) {
					hops = hops + 1;
					return true;
				}

			}
		}

		if (!found) {
			if (!(sr+1==prevI && (sc)==prevJ) && inBounds(sr+1,sc) &&(maze[sr + 1][sc] == '1')) {

				found = analyseMaze(sr + 1, sc, sr, sc);//south
				if (found) {
					hops = hops + 1;
					return true;
				}

			}
		}

		return false;

		// else {
		// roadblock = true;
		// hops = hops - 1;
		// return false;
		// }
		//
		// if (roadblock == true) {
		//
		// roadblock = false;
		// analyseMaze(sr, sc);
		//
		// }

		// maze[sr][sc] = '*';
		//
		// if (maze[sr][sc - 1] == '1') {
		//
		// analyseMaze(sr, sc - 1);
		// hops = hops + 1;
		//
		// } else if (maze[sr - 1][sc] == '1') {
		//
		// analyseMaze(sr - 1, sc);
		// hops = hops + 1;
		//
		// } else if (maze[sr][sc + 1] == '1') {
		//
		// analyseMaze(sr, sc + 1);
		// hops = hops + 1;
		//
		// } else if (maze[sr + 1][sc] == '1') {
		//
		// analyseMaze(sr + 1, sc);
		// hops = hops + 1;
		//
		// } else {
		//
		// roadblock = true;
		// hops = hops - 1;
		// return;
		//
		// }
		//
		// if (roadblock == true) {
		//
		// roadblock = false;
		// analyseMaze(sr, sc);
		// hops = hops + 1;
		//
		// }
		//
		// if (found == true) {
		//
		// maze[sr][sc] = '+';
		//
		// }

	}

	private boolean inBounds(int sr, int sc) {
	
		return sr >=0 && sr< maze.length && sc >=0 && sc< maze[0].length;
	}

	private static void printMaze(char[][] maze) {
		for (int i = 0; i < maze.length; i++) {
			for (int j = 0; j < maze[i].length; j++) {

				System.out.print(maze[i][j]);

			}
			System.out.println();
		}

	}

}
